package cn.zhuizikejithreeig.com.xigedome;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ProgressbackView mProgressbackView;
    private ProgressView mProgressView;
    private ProgressbackView mImageOne;
    private ProgressbackView mImageTwo;
    private RelativeLayout mHandrelay;
    private TextView mTimeText;
    private LinearLayout mMiddleLine;
    private TextView mMiddleText;
    private Button mBiginButton;
    private Button mStopButton;
    private Button mContiun;
    private Button mFoger;
    private LinearLayout mButtonLine;
    private WheelView mWheelView;
    private List<String> list = new ArrayList<>();
    private int endTime = 0;
    private int startTime = 0;
    private boolean isStart = false;

    //记录当前的选择值
    private int mins = 7 * 60 * 1000;
    private Animation animationTwo;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    startTime = startTime - 1000;
                    if (startTime <= 0) {
                        mTimeText.setText("倒计时结束");
                        endTime = 0;
                        return;
                    }
                    endTime = startTime;
                    mTimeText.setText(timeChenge(startTime));
                    break;

            }
        }
    };

    //动画开始
    private Animation animationOne;
    //播放音乐
    private MediaPlayer player;
    private int selectTime = 7 * 60 * 1000;
    private ProgressbackView mImageThree;
    private Animation animationThree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //圆形进度圈以及进度圈背景
        mProgressbackView = (ProgressbackView) findViewById(R.id.progressbackview);
        mProgressView = (ProgressView) findViewById(R.id.progressview);

        //圆形进度圈扩散圈
        mImageOne = (ProgressbackView) findViewById(R.id.progressbackone);
        mImageTwo = (ProgressbackView) findViewById(R.id.progressbacktwo);
        mImageThree = (ProgressbackView) findViewById(R.id.progressbackthree);
        mImageOne.setPaintColor("#ffffff");
        mImageTwo.setPaintColor("#ffffff");
        mImageThree.setPaintColor("#ffffff");
        //中间的相对布局
        mHandrelay = (RelativeLayout) findViewById(R.id.handrelay);

        //倒计时时间
        mTimeText = (TextView) findViewById(R.id.text);


        //初始化界面,问候文字
        mMiddleLine = (LinearLayout) findViewById(R.id.start);
        mMiddleText = (TextView) findViewById(R.id.middletext);


        //底部的四个button
        mBiginButton = (Button) findViewById(R.id.first);
        mStopButton = (Button) findViewById(R.id.second);
        mContiun = (Button) findViewById(R.id.contiun);
        mFoger = (Button) findViewById(R.id.foger);

        //显示两个button的根布局
        mButtonLine = (LinearLayout) findViewById(R.id.thiredline);

        //选择时间滚轮
        mWheelView = (WheelView) findViewById(R.id.wheelview);
        //初始化设置
        init();

        //设置按钮的点击事件
        mBiginButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mContiun.setOnClickListener(this);
        mFoger.setOnClickListener(this);
        mHandrelay.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击开始专注
            case R.id.first:
                initStart();
                break;
            //当点击暂停的时候
            case R.id.second:
                initStop();
                break;
            //当点击继续的时候
            case R.id.contiun:

                initRestart();
                break;

            //当点击放弃的时候
            case R.id.foger:

                initabandon();
                break;
            //当点击顶部圆圈时
            case R.id.handrelay:
                clickHand();
                break;
        }
    }

    //当点击放弃的时候
    private void initabandon() {

        //设置进度条等可以开始播放播放,暂停倒计时
        CompontApplayCation.appKey = false;
        //设置头像不可点击
        mHandrelay.setEnabled(true);
        //底部的四个按钮开始变化
        mBiginButton.setVisibility(View.VISIBLE);
        mStopButton.setVisibility(View.GONE);
        mButtonLine.setVisibility(View.GONE);
        //进入初始化状态
        //暂停音乐
        stopMusic();
        //设置进度条不可见
        mProgressView.setVisibility(View.INVISIBLE);

        //中间的内容部分不可见
        mWheelView.setVisibility(View.INVISIBLE);
        mTimeText.setVisibility(View.INVISIBLE);
        mMiddleLine.setVisibility(View.VISIBLE);

        //选择表要进行隐藏
        mWheelView.setVisibility(View.INVISIBLE);

        //设置动画停止
        stopAnmiton();


    }

    private void stopAnmiton() {
        mImageOne.clearAnimation();
        mImageTwo.clearAnimation();
        mImageThree.clearAnimation();
        animationOne = AnimationUtils.loadAnimation(MainActivity.this, R.anim.stop_animation);
        animationTwo = AnimationUtils.loadAnimation(MainActivity.this, R.anim.stop_animation);
        animationThree = AnimationUtils.loadAnimation(MainActivity.this, R.anim.stop_animation);
        mImageOne.setVisibility(View.INVISIBLE);
        mImageTwo.setVisibility(View.INVISIBLE);
        mImageThree.setVisibility(View.INVISIBLE);
    }

    //当点击继续的时候
    private void initRestart() {
        //设置进度条不能播放,暂停倒计时
        CompontApplayCation.appKey = false;

        //设置头像不可点击
        mHandrelay.setEnabled(false);

        //记录开始防止其他复现
        isStart = true;

        //底部的四个按钮开始变化
        mBiginButton.setVisibility(View.GONE);
        mStopButton.setVisibility(View.VISIBLE);
        mButtonLine.setVisibility(View.GONE);

        //其他界面的
        mMiddleLine.setVisibility(View.INVISIBLE);
        mTimeText.setVisibility(View.VISIBLE);
        mMiddleLine.setVisibility(View.INVISIBLE);


        //开始动画
        mImageOne.setVisibility(View.VISIBLE);
        mImageTwo.setVisibility(View.VISIBLE);
        mImageThree.setVisibility(View.VISIBLE);

        //选择表要进行隐藏
        mWheelView.setVisibility(View.INVISIBLE);

        //开始播放动画
        initAnimation();
        //开始播放音乐
        initPalyMusic();
        //开始统计倒计时
        initTimeCleam(endTime);


        //开始进度条
        mProgressView.continueDoTime(new OnFinishListener() {
            @Override
            public void onFinish() {
                mins = selectTime;

                //结束音乐和动画
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //动画暂停
                        stopAnmiton();
                        //暂停音乐
                        stopMusic();
                    }
                });
            }
        });


    }

    //当点击暂停的时候
    private void initStop() {
        //设置进度条不能播放,暂停倒计时
        CompontApplayCation.appKey = true;

        //当点击开始的时候,首先要设置头像点击不可用
        mHandrelay.setEnabled(false);

        //底部的四个按钮开始变化
        mBiginButton.setVisibility(View.GONE);
        mStopButton.setVisibility(View.GONE);
        mButtonLine.setVisibility(View.VISIBLE);

        //其他界面的
        mMiddleLine.setVisibility(View.INVISIBLE);
        mTimeText.setVisibility(View.VISIBLE);
        mMiddleLine.setVisibility(View.INVISIBLE);

        //选择表要进行隐藏
        mWheelView.setVisibility(View.INVISIBLE);

        //动画暂停
        stopAnmiton();

        //暂停音乐
        stopMusic();

    }

    private void stopMusic() {
        if (player != null) {
            player.pause();
        }
    }

    //当点击开始的时候
    private void initStart() {

        //首先呢要设置可以点击进行动画以及可以进行走进度圈
        CompontApplayCation.appKey = false;

        //点击开始了
        isStart = true;
        //当点击开始的时候,首先要设置头像点击不可用
        mHandrelay.setEnabled(false);

        //底部的四个按钮开始变化
        mBiginButton.setVisibility(View.GONE);
        mStopButton.setVisibility(View.VISIBLE);
        mButtonLine.setVisibility(View.GONE);

        //隐藏问候语
        mMiddleLine.setVisibility(View.INVISIBLE);
        mTimeText.setVisibility(View.VISIBLE);

        //选择表隐藏
        mWheelView.setVisibility(View.INVISIBLE);


        //设置进度条可以走了
        mProgressView.setVisibility(View.VISIBLE);
        mProgressView.setPaintColor("#FF4081");
        mProgressView.startDownTime(mins, new OnFinishListener() {
            @Override
            public void onFinish() {
                mins = selectTime;
                //结束音乐和动画
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //动画暂停
                        stopAnmiton();

                        //暂停音乐
                        stopMusic();
                    }
                });
            }
        });

        //开始播放动画
        initAnimation();
        //开始播放音乐
        initPalyMusic();


        //开始统计倒计时,继续上次停下来的时间开始
        initTimeCleam(mins);


    }

    //设置开始倒计时
    private void initTimeCleam(int minsSelcet) {
        startTime = minsSelcet;
        mTimeText.setText(timeChenge(minsSelcet));
        handler.removeCallbacksAndMessages(null);
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (!CompontApplayCation.appKey) {
                    Message message = handler.obtainMessage(1);
                    handler.sendMessage(message);
                    handler.postDelayed(this, 1000);
                } else {
                    return;
                }
            }
        });
    }

    private void initPalyMusic() {
        if (player == null) {
            try {
                AssetManager assetManager = this.getAssets();
                AssetFileDescriptor afd = assetManager.openFd("hailang.mp3");
                player = new MediaPlayer();
                player.setDataSource(afd.getFileDescriptor(),
                        afd.getStartOffset(), afd.getLength());
                player.setLooping(true);//循环播放
                player.prepare();
                player.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            player.start();
        }
    }

    private void initAnimation() {
        animationOne = AnimationUtils.loadAnimation(MainActivity.this, R.anim.player_double_click_animation);
        mImageOne.startAnimation(animationOne);
        animationOne.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (!CompontApplayCation.appKey) {
                            SystemClock.sleep(2000);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (!CompontApplayCation.appKey) {
                                        animationTwo = AnimationUtils.loadAnimation(MainActivity.this, R.anim.player_double_click_animation);
                                        mImageTwo.startAnimation(animationTwo);
                                        new Thread(new Runnable() {
                                            @Override
                                            public void run() {
                                                SystemClock.sleep(2000);
                                                runOnUiThread(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        if (!CompontApplayCation.appKey) {
                                                            animationThree = AnimationUtils.loadAnimation(MainActivity.this, R.anim.player_double_click_animation);
                                                            mImageThree.startAnimation(animationThree);
                                                        }
                                                    }
                                                });
                                            }
                                        }).start();
                                    }
                                }
                            });
                        }
                    }
                }).start();
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (!CompontApplayCation.appKey) {
                    mImageOne.startAnimation(animationOne);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


    private void clickHand() {
        mMiddleLine.setVisibility(View.INVISIBLE);
        mWheelView.setVisibility(View.VISIBLE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(2000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!isStart) {
                            mMiddleLine.setVisibility(View.VISIBLE);
                            mWheelView.setVisibility(View.INVISIBLE);
                        }
                    }
                });
            }
        }).start();
        mins = 1 * 60 * 1000;
        selectTime = 1 * 60 * 1000;
        isStart = false;
    }

    private void init() {
        //进度圈不可见
        mProgressView.setVisibility(View.INVISIBLE);
        mProgressbackView.setVisibility(View.VISIBLE);
        mProgressbackView.setPaintColor("#e4fff2");


        //底部的按钮不可见
        mBiginButton.setVisibility(View.VISIBLE);
        mStopButton.setVisibility(View.GONE);
        mButtonLine.setVisibility(View.GONE);


        //中间的内容部分不可见
        mWheelView.setVisibility(View.INVISIBLE);
        mTimeText.setVisibility(View.INVISIBLE);


        //初始化下拉列表数据
        list.clear();
        list.add("01分钟");
        list.add("02分钟");
        list.add("03分钟");
        list.add("04分钟");
        list.add("05分钟");
        list.add("06分钟");
        list.add("07分钟");
        mWheelView.setCyclic(false);
        mWheelView.setAdapter(new ArrayWheelAdapter(list));
        mWheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {

                if (TextUtils.equals(list.get(index), "01分钟")) {
                    mins = 1 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "02分钟")) {
                    mins = 2 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "03分钟")) {
                    mins = 3 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "04分钟")) {
                    mins = 4 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "05分钟")) {
                    mins = 5 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "06分钟")) {
                    mins = 6 * 60 * 1000;
                    selectTime = mins;
                } else if (TextUtils.equals(list.get(index), "07分钟")) {
                    mins = 7 * 60 * 1000;
                    selectTime = mins;
                }
                System.out.println("当前的分钟数" + mins);
            }
        });
    }

    // 位数不足补零
    private String fill_zero_prefix(int num) {
        return num < 10 ? "0" + num : num + "";
    }

    private String timeChenge(int time) {
        // 秒数
        int second = (time / 1000);
        //小时数
        int hr = second / 3600;
        //分钟位
        String min = fill_zero_prefix((int) (second - hr * 3600) / 60);
        // 秒位
        String sec = fill_zero_prefix((second - hr * 3600 - Integer.parseInt(min) * 60));
        return min + ":" + sec;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
